package com.juyu.miliao.api.util.pay;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer;
import com.juyu.miliao.common.domain.AlipayConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ZfbPayUtil {
	// 支付请求地址
	 public static final String alipay_url = "https://openapi.alipay.com/gateway.do";
	// 沙箱地址
//	public static final String alipay_url = "http://openapi.alipaydev.com/gateway.do";
	 //新配置 申请中
//	public static final String app_id = "2018083061110873";
//	// 应用私钥
//	public static final String app_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCfJUFIOvwHDDq9igkEBNTnO86nIyLNWDC1XLzmONu2VcEhpcfThiOppyULlDdTJOGFivux1wU2INfoRbWKz42vykC2NyOxJbUgbdLuvFi5OFNYsLubhroYFs/dYCfcqVR2hU1PTf3Vh2l0b3crU5nUuN8mE3uJ2QCcbwkGL9SkbZfQrtLq5sl1m4Ni/NUWg68XXS9lbOOq6cZXOekAo2zguznEZO/a3vjOqvYddbAE1QLKev6w9Fb44ZXk9aJoi2UxjVLbUZDZDsiwWzHIMLekOx3uKro6mxFWTMmIx+4Z8ocmNXOCAeJc/4FXdnK0ztY4Ahu6mTZRYYoGOX3E4yx1AgMBAAECggEBAIVRW0fcCccJqqefXAAe4A+5nVuMZpoo99zmjGQ8W2p6mijKzQRYycVUfQcfKd2f9sC9di4712TJzLUKuJNJXgNWZjF/LYo38WzyM5LE+3pmQgP5QaB/E6QccBOeeqYyv8vtzZ7S0gKvxlrBCc6k4Sg/msg15/bJb+acaa3iIbt/ri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MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAumuRzL/E3uuU+QqPZGzq/hF+39AXDfBbaolTI4yusNPpR0pxN39Wjutjsao+rMAnd4e+f/O011hR+yH9lO6xYuzN1AMzpP1g/aeWYyKdgX2hCxVbSEQYmD73seHVX3INzGZvvDjjFgEQD1PLS3wjkgh41MuVVmDeA54kxNPVxHydCwytu8QcS0h9ii3z0/Zz+NaTV1b9LSaik3sqYBbumtt5lSIRhhfhLvXtV0H+vPCH7rlFxBYbR/oiCX17bHKQ574Zups4pZ237t7qpb+dx3XKT1/Q18p52INXlj4ToNINXFQVttwoNIA1CRuoE/obyqPFyPqYlXz1JHxw9GebZQIDAQAB";
	 
	//老配置
//	// 应用私钥
//	public static final String app_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDPmkBhLXkH4MHaFmuVSzovwpQ8OarInIkjtZc5f3knjoJED8iFIcFXLemzux4+4kJR8KorotjGE4cZtOTmILPJ3SF9mQ+d/DOWa3oUOmYOeow3egH7RHAUKmZ2qKfgawxs+vgxNaWXERXKGxx2lEmncyttYz8Q4eVSF6Hr/DqDaVs3ebUs5nQuKsraBWkDEIYjqkMNaQPFff7+tBwV5/sNxNAAtvgt0E3McOL8a1RBB5J4RQuNjWnq3aajDQX/4sc4W/NrZJWpR4ByjDrVr3/R8UFL1TvRVql+wmY0o/VjjUF66cT0eWHZ5cS3X+m0wKYamTRH860aPCdQByEoURN3AgMBAAECggEBALMb4/tzXf5b/SOJQFxXwOW/vGCRaDUYSIFUhBoJTp/AJcdOzrN9we7TH9P+1kNutcU4G7j2EIwkAOU+2lVpVXQNon1X9s9Y7bETHQoisDZBqIOcceU00HW1dx1cuSm3ccr8Y/Eg1bKV/PSXm2D64ujjRk5j199a5ZyD4yFURa9lQc1rKYQFDqiidtt6i+QoPvY5YOpQicJVZ4D2UbHK5zmDtihV+yGcZ2JiBxOzD9+5arXm6AFBU26UmMZRKTUjQ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ct7fTbjL5mcrlVqUb7rGOGkAhpBN5JNn8Rd9CfLL6RH7FpSUoTT3lj/bp1z1d3ce51ZD9N9iENY97FmZmMJ8ipEN9lb/aipMlFDk6EyqVnBQrfp+1MAGdhaS4mn5XvxMjhmdDRYdAWeQuk6u40EAB5Vqbz57mWu6G9YKosOop7PH2toCLt0qLEtBYz/gxf1X+KkXdUPVEZFqQZtwT9HCT2t6iMlxu0b6+lHWkegUJy3gRmz/D8eUYE78UfLW4ADAOQjIbJ4+xBjXFqVZ9VVx8HoPveWjWITT1iMwdpnwmNPV9Yt8wRDnLwIDAQAB";
//
//	// 同步地址
//	public static final String notify_url = "https://pay.goliaoyou.com/pay/notify/zfb_notify";
//	public static final String app_id = "2018042002585543";
//	public static final String notify_url = "http://test.api.goliaoyou.com:10000/pay/zfb_notify";
	private final static Logger payNotifyLogger = LoggerFactory.getLogger("payNotify");
	
	
	public static String aliPay(String body,String title,String orderId,String fee,String passback_params){
		// 实例化客户端
		AlipayClient alipayClient = new DefaultAlipayClient(alipay_url, AlipayConfig.getAppId(), AlipayConfig.getAppPrivateKey(), "json", "utf-8",
				AlipayConfig.getAlipayPublicKey(), "RSA2");
		// 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
		AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
		// SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
		AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
		model.setBody(body);// 对一笔交易的具体描述信息。如果是多种商品，请将商品描述字符串累加传给body。："我是测试数据"
		model.setSubject(title); // 商品的标题/交易标题/订单标题/订单关键字等。："App支付测试Java"
		model.setOutTradeNo(orderId); // 商户网站唯一订单号
		model.setTimeoutExpress("30m");// 该笔订单允许的最晚付款时间，逾期将关闭交易。取值范围：1m～15d。m-分钟，h-小时，d-天，1c-当天
		//上线改成fee
		model.setTotalAmount(fee); // 订单总金额，单位为元，精确到小数点后两位，取值范围[0.01,100000000]："0.01"-fee
		model.setProductCode("QUICK_MSECURITY_PAY"); // 销售产品码，商家和支付宝签约的产品码，为固定值QUICK_MSECURITY_PAY
		model.setPassbackParams(passback_params);
		request.setBizModel(model);
		request.setNotifyUrl(AlipayConfig.getNotifyUrl());// 商户外网可以访问的异步地址
		String temp = "";
		try {
			// 这里和普通的接口调用不同，使用的是sdkExecute
			AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
			System.out.println(response.getBody());// 就是orderString可以直接给客户端请求，无需再做处理。
			temp = response.getBody();
		} catch (Exception e) {
			payNotifyLogger.error("支付宝支付调用请求错误",e);
		}
		return temp;
	}


	public static AlipayTradeWapPayResponse aliWapPay(String body,String title,String orderId,String fee,String passback_params){
		// 实例化客户端
		AlipayClient alipayClient = new DefaultAlipayClient(alipay_url, AlipayConfig.getAppId(), AlipayConfig.getAppPrivateKey(), "json", "utf-8",
				AlipayConfig.getAlipayPublicKey(), "RSA2");
		// 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
		AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
		// SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
		AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
		model.setBody(body);// 对一笔交易的具体描述信息。如果是多种商品，请将商品描述字符串累加传给body。："我是测试数据"
		model.setSubject(title); // 商品的标题/交易标题/订单标题/订单关键字等。："App支付测试Java"
		model.setOutTradeNo(orderId); // 商户网站唯一订单号
		model.setTimeoutExpress("30m");// 该笔订单允许的最晚付款时间，逾期将关闭交易。取值范围：1m～15d。m-分钟，h-小时，d-天，1c-当天
		//上线改成fee
		model.setTotalAmount(fee); // 订单总金额，单位为元，精确到小数点后两位，取值范围[0.01,100000000]："0.01"-fee
		model.setProductCode("QUICK_MSECURITY_PAY"); // 销售产品码，商家和支付宝签约的产品码，为固定值QUICK_MSECURITY_PAY
		model.setPassbackParams(passback_params);
		request.setBizModel(model);
		request.setNotifyUrl(AlipayConfig.getNotifyUrl());// 商户外网可以访问的异步地址
		String temp = "";
		try {
			// 这里和普通的接口调用不同，使用的是sdkExecute
			AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
			return response;
		} catch (Exception e) {
			payNotifyLogger.error("支付宝支付调用请求错误",e);

		}
		return null;
	}
}
